package com.ruoyi.web.controller.ora;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ora.service.IDeviceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.DeviceMigration;
import com.ruoyi.ora.service.IDeviceMigrationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备迁移Controller
 * 
 * @author liuwp
 * @date 2025-10-12
 */
@RestController
@RequestMapping("/ora/device_migration")
public class DeviceMigrationController extends BaseController
{
    @Autowired
    private IDeviceMigrationService deviceMigrationService;
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备迁移列表
     */
    @PreAuthorize("@ss.hasPermi('ora:device_migration:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceMigration deviceMigration)
    {
        startPage();
        List<DeviceMigration> list = deviceMigrationService.selectDeviceMigrationList(deviceMigration);
        return getDataTable(list);
    }

    /**
     * 导出设备迁移列表
     */
    @PreAuthorize("@ss.hasPermi('ora:device_migration:export')")
    @Log(title = "设备迁移", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceMigration deviceMigration)
    {
        List<DeviceMigration> list = deviceMigrationService.selectDeviceMigrationList(deviceMigration);
        ExcelUtil<DeviceMigration> util = new ExcelUtil<DeviceMigration>(DeviceMigration.class);
        util.exportExcel(response, list, "设备迁移数据");
    }

    /**
     * 获取设备迁移详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:device_migration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceMigrationService.selectDeviceMigrationById(id));
    }

    /**
     * 新增设备迁移
     */
    @PreAuthorize("@ss.hasPermi('ora:device_migration:add')")
    @Log(title = "设备迁移", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceMigration deviceMigration)
    {
        return toAjax(deviceMigrationService.insertDeviceMigration(deviceMigration));
    }

    /**
     * 修改设备迁移
     */
    @PreAuthorize("@ss.hasPermi('ora:device_migration:edit')")
    @Log(title = "设备迁移", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceMigration deviceMigration)
    {
        return toAjax(deviceMigrationService.updateDeviceMigration(deviceMigration));
    }

    /**
     * 删除设备迁移
     */
    @PreAuthorize("@ss.hasPermi('ora:device_migration:remove')")
    @Log(title = "设备迁移", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceMigrationService.deleteDeviceMigrationByIds(ids));
    }

    @GetMapping("/query")
    public AjaxResult query(DeviceMigration deviceMigration)
    {
        deviceMigration.setUserId(SecurityUtils.getUserId());
        List<DeviceMigration> list = deviceMigrationService.selectDeviceMigrationList(deviceMigration);
        list.forEach(item->{
            item.setFromDevice(deviceService.selectDeviceById(item.getFromDeviceId()));
            item.setToDevice(deviceService.selectDeviceById(item.getToDeviceId()));
        });
        return AjaxResult.success(list);
    }

}
